home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / test / polytest.c < prev    next >
Text File  |  1993-12-06  |  5KB  |  182 lines

  1. /**
  2.  ** POLYTEST.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "test.h"
  25. #include <string.h>
  26.  
  27. static int red;
  28.  
  29. #ifdef __GNUC__
  30. #define clock() rawclock()
  31. #endif
  32.  
  33. extern long clock();
  34.  
  35. unsigned char mybits[] = { 0x88, 0x44, 0x22, 0x11 };
  36. GrBitmap mybitmap = { 0, 4, mybits };
  37.  
  38. GrPattern *p1;
  39. GrPattern *p2 = (GrPattern *)&mybitmap;
  40.  
  41. static void initpatterns(void)
  42. {
  43.     GrContext tmp,save;
  44.     GrFBoxColors bc;
  45.  
  46.     mybitmap.bmp_fgcolor = GrAllocColor(255,0,0);
  47.     mybitmap.bmp_bgcolor = GrNOCOLOR;
  48.  
  49.     GrCreateContext(20,20,NULL,&tmp);
  50.     GrSaveContext(&save);
  51.     GrSetContext(&tmp);
  52.     bc.fbx_intcolor = GrAllocColor(0,140,140);
  53.     bc.fbx_leftcolor = bc.fbx_topcolor = GrAllocColor(0,200,200);
  54.     bc.fbx_rightcolor = bc.fbx_bottomcolor = GrAllocColor(0,80,80);
  55.     GrFramedBox(2,2,17,17,2,&bc);
  56.     p1 = GrConvertToPixmap(&tmp);
  57.     GrSetContext(&save);
  58. }
  59.  
  60. static void testpoly(int n,int points[][2])
  61. {
  62.     GrClearScreen(GrBlack());
  63.     GrSetClipBox(100,100,700,500);
  64.     GrFilledPolygon(n,points,red);
  65.     GrSetClipBox(0,0,GrMaxX(),GrMaxY());
  66.     GrBox(99,99,701,501,GrWhite() | GrXOR);
  67.     GrPolygon(n,points,GrWhite() | GrXOR);
  68.     getkey();
  69.     GrClearScreen(GrBlack());
  70.     GrPatternFilledPolygon(n,points,p1);
  71.     getkey();
  72.     GrPatternFilledPolygon(n,points,p2);
  73.     getkey();
  74.     if(n <= 3) {
  75.         GrClearScreen(GrBlack());
  76.         GrFilledPolygon(n,points,red);
  77.         GrFilledConvexPolygon(n,points,GrWhite() | GrXOR);
  78.         getkey();
  79.     }
  80. }
  81.  
  82. static void speedtest(void)
  83. {
  84.     int pts[4][2];
  85.     int ww = GrSizeX() / 10;
  86.     int hh = GrSizeY() / 10;
  87.     int sx = (GrSizeX() - 2*ww) / 32;
  88.     int sy = (GrSizeY() - 2*hh) / 32;
  89.     int ii,jj,color;
  90.     long t1,t2,t3;
  91.  
  92.     GrClearScreen(GrBlack());
  93.     t1 = clock();
  94.     pts[0][1] = 0;
  95.     pts[1][1] = hh;
  96.     pts[2][1] = 2*hh;
  97.     pts[3][1] = hh;
  98.     color = 0;
  99.     for(ii = 0; ii < 32; ii++) {
  100.         pts[0][0] = ww;
  101.         pts[1][0] = 2*ww;
  102.         pts[2][0] = ww;
  103.         pts[3][0] = 0;
  104.         for(jj = 0; jj < 32; jj++) {
  105.         GrFilledPolygon(4,pts,color | GrXOR);
  106.         color = (color + 1) & 15;
  107.         pts[0][0] += sx;
  108.         pts[1][0] += sx;
  109.         pts[2][0] += sx;
  110.         pts[3][0] += sx;
  111.         }
  112.         pts[0][1] += sy;
  113.         pts[1][1] += sy;
  114.         pts[2][1] += sy;
  115.         pts[3][1] += sy;
  116.     }
  117.     t2 = clock();
  118.     pts[0][1] = 0;
  119.     pts[1][1] = hh;
  120.     pts[2][1] = 2*hh;
  121.     pts[3][1] = hh;
  122.     color = 0;
  123.     for(ii = 0; ii < 32; ii++) {
  124.         pts[0][0] = ww;
  125.         pts[1][0] = 2*ww;
  126.         pts[2][0] = ww;
  127.         pts[3][0] = 0;
  128.         for(jj = 0; jj < 32; jj++) {
  129.         GrFilledConvexPolygon(4,pts,color | GrXOR);
  130.         color = (color + 1) & 15;
  131.         pts[0][0] += sx;
  132.         pts[1][0] += sx;
  133.         pts[2][0] += sx;
  134.         pts[3][0] += sx;
  135.         }
  136.         pts[0][1] += sy;
  137.         pts[1][1] += sy;
  138.         pts[2][1] += sy;
  139.         pts[3][1] += sy;
  140.     }
  141.     t3 = clock();
  142.     sprintf(exit_message,
  143.         "Times to scan 1024 polygons\n"
  144.         "   with 'GrFilledPolygon': %.2f (s)\n"
  145.         "   with 'GrFilledConvexPolygon': %.2f (s)\n",
  146.         (double)(t2 - t1) / 18.2,
  147.         (double)(t3 - t2) / 18.2
  148.     );
  149. }
  150.  
  151. TESTFUNC(ptest)
  152. {
  153.     char buff[300];
  154.     int  pts[300][2];
  155.     int  ii,collect;
  156.     FILE *fp;
  157.  
  158.     fp = fopen("polytest.dat","r");
  159.     if(fp == NULL) return;
  160.     collect = 0;
  161.     red = GrAllocColor(255,0,0);
  162.     GrSetColor((red ^ GrWhite()),0,50,255);
  163.     initpatterns();
  164.     while(fgets(buff,299,fp) != NULL) {
  165.         if(!collect) {
  166.         if(strncmp(buff,"begin",5) == 0) {
  167.             collect = 1;
  168.             ii = 0;
  169.         }
  170.         continue;
  171.         }
  172.         if(strncmp(buff,"end",3) == 0) {
  173.         testpoly(ii,pts);
  174.         collect = 0;
  175.         continue;
  176.         }
  177.         if(sscanf(buff,"%d %d",&pts[ii][0],&pts[ii][1]) == 2) ii++;
  178.     }
  179.     speedtest();
  180. }
  181.  
  182.